home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10615 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: news.ichange.com!newsmaster
  2. From: Jesse Liberty <jl@staff.ichange.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Can somebody explain me how to do this in C++
  5. Date: Fri, 08 Mar 1996 15:27:35 -0500
  6. Organization: AT&T New Media Services
  7. Message-ID: <31409837.4AB2@staff.ichange.com>
  8. References: <4hkb2o$ko3@mo6.rc.tudelft.nl>
  9. NNTP-Posting-Host: 198.112.128.210
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win95; I)
  14.  
  15. Ejo Schrama wrote:
  16. > Suppose you've got a base class containing protected data and several
  17. > public methods among which there are some virtual ones. ...during the execution of a program...you only want to deal with a linked list of base class objects...There are
  18. > local variables declared as protected data inside the inhereted subclasses
  19. > (which don't exist in the baseclass) and I want to access those variables
  20. > in the while construction described above.
  21.  
  22. This is a well known design flaw not a language limitation.  There are many ways out of it, but first you have to undertsand 
  23. the flaw.  Let's say you have a linked list of animals. You want to treat them as animals, rather than as birds, horses and 
  24. fish. However, from time to time, you want to tell a fish to swim and a bird to fly.
  25.  
  26. You have a number of choices.  You can move a virtual function into the base class (e.g., Animal::move()) and then override 
  27. it appropriately in each derived class. That's fine if all animals can reasonably be expected to move().  This works, but 
  28. brings the risk that functions all percolate up into the base class. In general, you want to percolate shared functionality 
  29. up the hierarchy, without migrating the interface of each class into the base.
  30.  
  31. In any case, what do you do if there are methods which don't belong in every derived class?
  32.  
  33. Again, a few options. You can have a method makeAHome() which makes a nest in a bird, digs a hole in a mouse and does nothing 
  34. in a horse object, but feh.  A second option is to "cast down" to the derived class. That is, in your loop you can ask each 
  35. object if it is a bird, and if it is you can cast it to a bird and then call bird-only methods. This is usually a sign of 
  36. very poor design, but sometimes it is the only way to get it done.
  37.  
  38. These issues often come up with regard to collection classes, which is where templates can help a lot.  Other issues arise 
  39. because of single inheritance.  Here multiple inheritance and delegation can often solve the problem.
  40.  
  41. Now, you also added a red herring with the "protected data" issue.  I'm not convinced it ever makes sense to have protected 
  42. data (protected methods, absolutely, but not member data).  The right answer is to make the data PRIVATE.  Then, if you need 
  43. access, use public or virtual & protected access methods.
  44.  
  45. Hope that helps, if not give a holler.  -j
  46.  
  47.  
  48. -- 
  49. Jesse Liberty. (AT&T New Media Services) jl@staff.ichange.com
  50. Teach Yourself C++ In 21 Days. SAMS, 1994
  51. Teach Yourself MORE C++ In 21 Days. SAMS, 1996
  52. Teach Yourself ANSI C++ In 21 Days. SAMS, 1996
  53. C++: An Introduction To Programming. QUE, 1996
  54.